# Copyright 2008 Kaspars Dancis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging
from datetime import datetime

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


def escape(text):    
    return text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')

def escapeCDATA(text):
    return u"""<![CDATA[""" + text + u"""]]>"""
    #return text

def from_iso(s):
    #dt = datetime.strptime(s[:19], "%Y-%m-%dT%H:%M:%S")
    dt = s
    logger.debug("====================")
    logger.debug("====================")
    logger.debug("date time: from_iso")
    logger.debug(str(dt))
    logger.debug("s? : " + s[:19])
    try: 
        #dt = datetime.strptime(s[:19], "%Y-%m-%dT%H:%M:%S.%f")
        dt = datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%f")
        logger.debug(str(dt))
        logger.debug("success 1")
        logger.debug("====================")
        logger.debug("====================")        
        return dt
    except:
        try: 
            #dt = datetime.strptime(s[:19], "%Y-%m-%d %H:%M:%S")
            dt = datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
            logger.debug(str(dt))
            logger.debug("success 2")
            logger.debug("====================")
            logger.debug("====================")
            return dt
        except: pass

    #2008-03-03T21:10:55
    try: 
        #dt = datetime.strptime(s[:19], "%Y-%m-%d %H:%M:%S")
        dt = datetime.strptime(s, "%Y-%m-%dT%H:%M:%S")
        logger.debug(str(dt))
        logger.debug("success 3.1")
        logger.debug("====================")
        logger.debug("====================")
        return dt
    except:
        try:
            dt = datetime.strptime(s[:19], "%Y-%m-%dT%H:%M:%S")
            logger.debug(str(dt))
            logger.debug("success 3.2")
            logger.debug("====================")
            logger.debug("====================")
            return dt            
        except: 
            pass
        
    #'2009-11-19 18:39:51'
    try: 
        dt = dt.replace(microsecond = int(s[20:]))
        logger.debug(str(dt))
        logger.debug("success 3")
        logger.debug("====================")
        logger.debug("====================")
        return dt
    except: pass

    logger.debug(str(dt))
    logger.debug("fail")
    logger.debug("====================")
    logger.debug("====================")
    return None

def to_iso(dt):
    logger.debug("====================")
    logger.debug("====================")
    logger.debug("date time: to_iso")
    logger.debug(str(dt))
    #dt = datetime.strptime(str(dt), "%Y-%m-%d %H:%M:%S")
    try: 
        dt = datetime.strptime(str(dt), "%Y-%m-%d %H:%M:%S.%f")
        logger.debug(str(dt))
        logger.debug("success 1")
        logger.debug("====================")
        logger.debug("====================")
        return dt.isoformat()

    except:
        try: 
            dt = datetime.strptime(str(dt), "%Y-%m-%d %H:%M:%S")
            logger.debug(str(dt))
            logger.debug("success 2")
            logger.debug("====================")
            logger.debug("====================")
            return dt.isoformat()

        except: 
            logger.debug(str(dt))
            logger.debug("fail")
            logger.debug("====================")
            logger.debug("====================")
            return None
        
    #dt = datetime(dt)
    #return dt.isoformat()

def parse_date(dt):
    try: 
        dt = datetime.strptime(str(dt), "%Y-%m-%d %H:%M:%S.%f")
        logger.debug(str(dt))
        logger.debug("success 1")
        logger.debug("====================")
        logger.debug("====================")
        return dt

    except:
        try: 
            dt = datetime.strptime(str(dt), "%Y-%m-%d %H:%M:%S")
            logger.debug(str(dt))
            logger.debug("success 2")
            logger.debug("====================")
            logger.debug("====================")
            return dt

        except: 
            logger.debug(str(dt))
            logger.debug("fail")
            logger.debug("====================")
            logger.debug("====================")
            return None
        
class Log:
    def __init__(self, f):
        self.f = f
        
    def write(self, s):
        self.f.write(s)
        self.f.flush()